package com.springboot;

import com.springboot.bean.A;
import com.springboot.bean.Cat;
import com.springboot.bean.Dog;
import com.springboot.bean.Monster;
import com.springboot.config.BeanConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @SpringBootApplication :表示这是一个springboot的应用/项目
 * scanBasePackages = "com" 指定springboot需要扫描的包以及子包
 * 如果有多个包 可以scanBasePackages = {"com","xx.yy.zz","aa.bb"}
 */
@SpringBootApplication(scanBasePackages = "com")
public class MainApp {
    public static void main(String[] args) {
        //启动springboot的应用程序/项目
        ConfigurableApplicationContext ioc =
                SpringApplication.run(MainApp.class, args);
//        System.out.println("---------------------------------------------------");
//        如何查看ioc的注入组件
//        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
//        for (String beanDefinitionName : beanDefinitionNames) {
//            System.out.println("beanDefinitionName---" + beanDefinitionName);
//        }
//        System.out.println("---------------------------------------------------");
//        @Component、@Controller、 @Service、@Repository
//        说明: 这些在 Spring 中的传统注解仍然有效，通过这些注解可以给容器注入组件
//        A bean = ioc.getBean(A.class);
//        System.out.println("Abean=" + bean);
//        System.out.println("---------------------------------------------------");
//        测试在springboot项目中，依然可以使用spring的配置bean/注入bean/获取bean---start
//        ApplicationContext beans =
//                new ClassPathXmlApplicationContext("beans.xml");
//        Monster bean = beans.getBean(Monster.class);
//        System.out.println("bean=" + bean);
//        测试在springboot项目中，依然可以使用spring的配置bean/注入bean/获取bean---end
//        System.out.println("---------------------------------------------------");
//        Monster monster01 = ioc.getBean("monster01", Monster.class);
//        Monster monster02 = ioc.getBean("monster01", Monster.class);
//        System.out.println("monster01="+monster01+" "+monster01.hashCode());
//        System.out.println("monster02="+monster02+" "+monster02.hashCode());
//        System.out.println("---------------------------------------------------");
//        配置类bean也会被注入到容器中
//        BeanConfig bean = ioc.getBean(BeanConfig.class);
//        System.out.println("bean=" + bean);
//        System.out.println("---------------------------------------------------");
//        BeanConfig beanConfig = ioc.getBean(BeanConfig.class);
//        Monster monster1 = beanConfig.monster01();
//        Monster monster2 = beanConfig.monster01();
//        System.out.println("monster1="+monster1+" "+monster1.hashCode());
//        System.out.println("monster2="+monster2+" "+monster2.hashCode())
//        System.out.println("---------------------------------------------------");
//        //直接通过ioc.getBean() 获取 Bean, proxyBeanMethods 值并没有生效
//        Monster monster01 = ioc.getBean("monster01", Monster.class);
//        Monster monster02 = ioc.getBean("monster01", Monster.class);
//        System.out.println("monster01=" + monster01 + " " + monster01.hashCode());
//        System.out.println("monster02=" + monster02 + " " + monster02.hashCode());
//        System.out.println("---------------------------------------------------");
//        测试可以有多个bean
//        Monster monster02 = ioc.getBean("monster02", Monster.class);
//        Monster monster01 = ioc.getBean("monster01", Monster.class);
//        System.out.println("monster01=" + monster01 + " " + monster01.hashCode());
//        System.out.println("monster02=" + monster02 + " " + monster02.hashCode());
//        System.out.println("---------------------------------------------------");
//        测试@Import注解
//        Dog dogBean = ioc.getBean(Dog.class);
//        Cat catBean = ioc.getBean(Cat.class);
//        System.out.println("dogBean="+dogBean);
//        System.out.println("catBean="+catBean);
//        System.out.println("---------------------------------------------------");
//        测试@ConditionalOnMissingBean注解
//        Dog dog = ioc.getBean("dog01",Dog.class);
//        System.out.println("dog=" + dog);
//        System.out.println("---------------------------------------------------");
//        @ImportResource注解
         Monster monster03 = ioc.getBean("monster03", Monster.class);
         System.out.println("monster03="+monster03);
         System.out.println("monster03----"+ioc.containsBean("monster03"));
    }
}
